package com.studio49.exmple.controller.web.sys;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.studio49.exmple.common.constant.ForwardConstant;
import com.studio49.exmple.common.constant.SysConstant;
import com.studio49.exmple.common.util.PubDateUtils;
import com.studio49.exmple.core.controller.BaseController;
import com.studio49.exmple.core.entity.pub.PageView;
import com.studio49.exmple.entity.web.sys.Role;
import com.studio49.exmple.mapper.web.sys.RoleMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Controller
@RequestMapping("/sys/role")
public class RoleController extends BaseController {
	
	@Autowired
	private RoleMapper roleMapper;

	/**
	 * 列表页
	 * @return
	 */
	@RequestMapping("/list")
	public String list(){
		return ForwardConstant.SYSTEM_H + ForwardConstant.ROLE + ForwardConstant.LIST;
	}
	
	/**
	 * 分页查询
	 * @param keyWords
	 * @param pageNow
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("/findByPage")
	@ResponseBody
	public PageView findByPage(String keyWords, Integer pageNow, Integer pageSize){
		if(pageNow == null) {
			pageNow = 1;
		}
		PageHelper.startPage(pageNow, pageSize);
		Example example = new Example(Role.class);
		Criteria criteria = example.createCriteria();
		if(StringUtils.isNotBlank(keyWords)) {
			criteria.andLike("name", "%" + keyWords + "%");
		}
		List<Role> list = roleMapper.selectByExample(example);

		PageView pageView = new PageView(list);
		return pageView;
	}
	
	/**
     * 打开角色新增画面
     * 
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping("addUI")
    public String addUI(Model model) throws Exception {
        return ForwardConstant.SYSTEM_H + ForwardConstant.ROLE + ForwardConstant.ADD;
    }

    /**
     * 保存角色信息
     * 
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("addEntity")
    // 凡需要处理业务逻辑的.都需要记录操作日志
    public String addEntity(Role role) throws Exception {
        
    	Date now = PubDateUtils.getNow();
    	role.setCreateTime(now);
    	role.setModifyTime(now);
    	
    	roleMapper.insertSelective(role);
        return SysConstant.SUCCESS;
    }

    /**
     * 角色删除操作
     * 
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("deleteEntity")
    @Transactional
    public String deleteEntity(String ids) throws Exception {
        String[] ids_c = ids.split(",");
        
        for(String id : ids_c) {
        	roleMapper.deleteByPrimaryKey(id);
        }
        return SysConstant.SUCCESS;
    }

    /**
     * 打开角色编辑画面
     * 
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping("editUI")
    public String editUI(Long id, Model model, HttpServletRequest request) throws Exception {
    	Role role = roleMapper.selectByPrimaryKey(id);
    	model.addAttribute("role", role);
        return ForwardConstant.SYSTEM_H + ForwardConstant.ROLE + ForwardConstant.EDIT;
    }

    /**
     * 角色编辑保存
     * 
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("editEntity")
    public String editEntity(Role role) throws Exception {
    	Date now = PubDateUtils.getNow();
    	role.setModifyTime(now);
    	roleMapper.updateByPrimaryKeySelective(role);
        return SysConstant.SUCCESS;
    }

}
